package handle

import (
	"fmt"
	"gitee.com/general252/ffmpeg-tool/help"
	"github.com/gin-gonic/gin"
	"net/http"
	"path/filepath"
)

type HomeHandle struct {
}

func NewHomeHandle() *HomeHandle {
	return &HomeHandle{}
}

func (tis *HomeHandle) Upload(c *gin.Context) {
	f, err := c.FormFile("file")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": -1,
			"msg":  err.Error(),
		})
		return
	}

	fileId := fmt.Sprintf("%v%v", help.GetUID(), filepath.Ext(f.Filename))
	inputFilename := fmt.Sprintf("./uploads/%v", fileId)
	err = c.SaveUploadedFile(f, inputFilename)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": -1,
			"msg":  err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":   0,
		"msg":    "success",
		"fileId": fileId,
	})
}
